package thired.class11_digui;

public class Class06_CardsInLine {
    public static int win1(int[] arr){
        if(arr == null || arr.length == 0){
            return 0;
        }
        int first = f1(arr,0,arr.length - 1);
        int second = g1(arr,0,arr.length - 1);
        return Math.max(first,second);
    }
    public static int f1(int[] arr,int L,int R){
        if(L==R){
            return arr[L];
        }
        int p1 = arr[L] + g1(arr,L + 1,R);
        int p2 = arr[R] + g1(arr,L,R - 1);
        return Math.max(p1,p2);
    }
    public static int g1(int[] arr,int L,int R){
        if(L == R){
            return 0;
        }
        int p1 = f1(arr, L + 1, R);
        int p2 = f1(arr, L ,R - 1);
        return Math.min(p1,p2);
    }



    public static int win2(int[] arr){
        if(arr == null || arr.length == 0){
            return 0;
        }
        int N = arr.length;
       int[][] fmap = new int[N][N];
       int[][] gmap = new int[N][N];
        for (int i = 0; i < N; i++) {
            fmap[i][i] = arr[i];
        }

        for (int startCol = 1; startCol < N; startCol++) {
            int row = 0;
            int col = startCol;
            while(col < N){
                fmap[row][col] = Math.max(arr[row] + gmap[row + 1][col], arr[col] + gmap[row][col - 1]);
                gmap[row][col] = Math.min(fmap[row + 1][col],fmap[row][col - 1]);
                row++;
                col++;
            }
        }
        return Math.max(fmap[0][N - 1],gmap[0][N - 1]);
    }
//    public static int f1(int[] arr,int L,int R){
//        if(L==R){
//            return arr[L];
//        }
//        int p1 = arr[L] + g1(arr,L + 1,R);
//        int p2 = arr[R] + g1(arr,L,R - 1);
//        return Math.max(p1,p2);
//    }
//    public static int g1(int[] arr,int L,int R){
//        if(L == R){
//            return 0;
//        }
//        int p1 = f1(arr, L + 1, R);
//        int p2 = f1(arr, L ,R - 1);
//        return Math.min(p1,p2);
//    }


    public static void main(String[] args) {
        int[] arr = { 5, 7, 4, 5, 8, 1, 6, 0, 3, 4, 6, 1, 7 };
        System.out.println(win1(arr));
        System.out.println(win2(arr));
//        System.out.println(win3(arr));

    }
}
